$.fn.extend({
   layer:(function(){
	   var mask = $('<div id="popMask"></div>').css({
			left:0,
			top:0,
			width:'100%',
			height:'100%',
			position:'absolute',
			display:'none',
			'backgroundColor':'#ffffff',
			'opacity': 0.6,
			'z-index':999
		}).appendTo('body');
		return function(){
			var lastTime = +new Date();
			$(this).on('click', function(){
				if(+new Date() - lastTime < 500)return;
				lastTime = +new Date();
				$(this).loadHTML();
				return false;
		   });
	   };
   })(),
   loadHTML:(function(index){
	   return function(){
			var config = {};
			try{
				config = eval('('+$(this).attr('data')+')');
			}catch(e){return;}
			if(config.type !== 'ajax')return;
			var mask = $('#popMask');

			mask.show();
			$('<div class="popLayer"></div>').on('click', '.close,.closeBtn', function(){
				$(this).trigger('close');
				return false;
			}).on('close', function(){
				mask.hide();
				mask = null;
				$(this).remove();
			}).load(config.url, function(){
				var o = $(this);
				o.css({
					marginLeft: -(o.width()/2)+'px',
					marginTop: -(o.height()/2)+'px',
					'z-index': ++index,
					display: 'block'
				});
			}).appendTo('body');
		};
	})(999)
});


/*
var Layer ={};
function W$(i){return document.getElementById(i)}
Layer.box = function (){
	var con,m,close,flag=0;
	return {
		show:function(id,m1,alpha,content,ajax,dialog,loading,w,h,close){
			if(!flag){
				m=document.createElement('div'); m.id='mask';
				document.body.appendChild(m);
				if(ajax||dialog||content){
					con = document.createElement('div'); con.id = 'layerCon';
					document.body.appendChild(con);
				}
				flag=1;
			}
			if(id){
				this.showId(W$(id));
			}
			else if(content){
				layer=document.createElement('div'); layer.id='layer';
				con.appendChild(layer);
				this.psh(content,loading,w,h,layer);
			}
			else if(ajax){
			//p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){
						Layer.box.psh(x.responseText,loading,w,h);
					}
				};
				x.open('GET',ajax,1); x.send(null)
			}
			if(m1){
				this.mask(alpha);
			}
			var cId = W$('layerClose');
			if(cId){
				this.listener(cId,'click',function(){
					W$(id).style.display='none';
					m.style.display='none';
				});
			}
		},
		listener:function(node,eventType,handler){
			node = typeof node == "string" ? document.getElementById(node) : node;
			if(document.all){
				node.attachEvent("on"+eventType,callback);
			}
			else {
				node.addEventListener(eventType,callback,false);
			}
			function callback(e){
				handler.call(node,e);
			}
		},
		showId:function(id){
			id.style.display='block';
			var idCon = id.innerHTML;
			this.pos(id);
		},
		pos:function(id){
			//console.log('1');
			var t=(Layer.page.height()/2)-(id.offsetHeight/2); t=t<10?10:t;
			id.style.top=(t+Layer.page.top())+'px';
			id.style.left=(Layer.page.width()/2)-(id.offsetWidth/2)+'px'
		},
		hide:function(){
			m.style.display='none'
			//alert('1');
		},
		remove:function(){
			m.parentNode.removeChild(div);
			//alert('1');
		},
		addClose:function(a,b){
			if(b){
				b.innerHTML=a+'<i class="layer-close" id="lClose">&times;</i>';
			}
			else{
				con.innerHTML=a+'<i class="layer-close" id="lClose">&times;</i>';
			}
			this.listener(W$('lClose'),'click',function(){
				con.style.display='none';
				m.style.display='none';
				//this.remove();
			});
			//this.pos(con);
		},
		psh:function(a,b,w,h,d){
			if(b){
				if(!w||!h){

				}else if(con){
					this.addClose(a,d);
					this.showId(con);
					con.style.width=w+'px';
					con.style.height=h+'px';
					if(d){
						layer.style.width=w-42+'px';
						layer.style.height=h-42+'px';
					}
				}
			}else{
				con.style.backgroundImage='none';
			}
		},
		alpha:function(v){
			if(!v){
				m.style.opacity=0.6; m.style.filter='alpha(opacity=60)';
			}
			else{
				//var v=o+Math.ceil(Math.abs(a-o)/s)*d;\
				m.style.opacity=v/100; m.style.filter='alpha(opacity='+v+')';
			}

		},
		mask:function(a){
			m.style.display='block';
			m.style.height=Layer.page.theight()+'px';
			m.style.width=''; m.style.width=Layer.page.twidth()+'px'
			this.alpha(a);

		}
	}
}();
Layer.page=function(){
	return{
		top:function(){return document.body.scrollTop||document.documentElement.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight},
		theight:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight))
		},
		twidth:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();
*/
